package com.xjgc.common.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master",ignoreInvalidFields = true)
public class DbConn {

	private String driverClassName;
	private String url;
	private String username;
	private String password;

	private Connection conn = null;
	
	public Connection getConnetion() {			
        try
        {
			Class.forName(driverClassName);
			conn = DriverManager.getConnection(url, username, password);
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        } 
		return conn;
	}
	
	public void closeCn() {
		if(conn != null) {
    		try {
        		if(!conn.isClosed()) {
        			conn.close();
        		}
    		}catch(SQLException e) {
    		}
    	}
	}
	
}
